package schoolAndCollage;
import javax.persistence.*;
import java.lang.reflect.Array;
import java.util.ArrayList;

public class OneToManyExample {
    public static void main(String[] args) {
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("SchoolAndCollage");
        EntityManager em = emf.createEntityManager();

        em.getTransaction().begin();

        CollageEntity c1 = new CollageEntity();
        c1.setCo_id(1001);
        c1.setCo_name("计算机学院");

        CollageEntity c2 = new CollageEntity();
        c2.setCo_id(1002);
        c2.setCo_name("环境学院");

        em.persist(c1);
        em.persist(c2);

        ArrayList<CollageEntity> list = new ArrayList<>();
        list.add(c1);
        list.add(c2);

        SchoolEntity s1 = new SchoolEntity();
        s1.setSc_id(1001);
        s1.setSc_name("山东建筑大学");
        s1.setCollageEntity(list);

        em.persist(s1);

        em.getTransaction().commit();
        em.close();
        emf.close();

        System.out.println("成功");
    }
}
